package client

import (
	"context"
	"github.com/micro/go-micro/v2/client"
	"github.com/micro/go-micro/v2/client/grpc"

	"kop/framework/wrapper"
	"kop/pb"
	"kop/pb/service"
)

var TreasureService service.TreasureService

func init() {

	var c = grpc.NewClient(client.Wrap(wrapper.ClientLogWrapper))
	_ = c.Init()

	//s.Init(test.Flags()...)

	TreasureService = service.NewTreasureService("go.micro.srv.ring", c)
}

func GetGrades(roleID int32) []*pb.TreasureGrade {

	var resp, err = TreasureService.GetGrades(context.Background(), &service.RoleRequest{RoleID: roleID})
	if err != nil {
		panic(err)
	}
	return resp.Grades
}

func GetPoint(roleID, mapID int32) *pb.TreasureGrade {

	var treasure, err = TreasureService.GetGrade(context.Background(), &service.DefaultRequest{RoleID: roleID, Value: mapID})
	if err != nil {
		panic(err)
	}
	return treasure
}

func GetTreasure(roleID int32) *pb.Treasure {

	var treasure, err = TreasureService.GetTreasure(context.Background(), &service.RoleRequest{RoleID: roleID})
	if err != nil {
		panic(err)
	}
	return treasure
}

// 初始化夺宝副本
func Init(roleID, mapID int32) (*pb.Treasure, error) {

	return TreasureService.Init(context.Background(), &service.TreasureInitRequest{RoleID: roleID, MapID: mapID})
}

func Save(treasure *pb.Treasure) {

	var resp, err = TreasureService.Save(context.Background(), treasure)
	if err != nil {
		panic(err)
	}
	*treasure = *resp
}

func Explore(roleID, x, y int32) (*pb.Treasure, error) {

	return TreasureService.Explore(context.Background(), &service.ExploreRequest{RoleID: roleID, X: x, Y: y})
}

func Buff(treasure *pb.Treasure, index int32) error {

	var resp, err = TreasureService.Buff(context.Background(), &service.TreasureSBuffRequest{Index: index, Treasure: treasure})
	if err == nil {
		*treasure = *resp
	}

	return err
}

//func AddPoint(x, y int32, treasure *pb.Treasure) {
//
//	var resp, err = srv.AddPoint(context.Background(), &service.ExploreRequest{X: x, Y: y, Treasure: treasure})
//	if err != nil {
//		panic(err)
//	}
//	*treasure = *resp
//}

func Box(roleID, x, y int32) *pb.Treasure {

	var treasure, err = TreasureService.Box(context.Background(), &service.ExploreRequest{RoleID: roleID, X: x, Y: y})
	if err != nil {
		panic(err)
	}
	return treasure
}

func NPCWin(roleID, x, y int32) *pb.Treasure {

	var treasure, err = TreasureService.NPCWin(context.Background(), &service.ExploreRequest{RoleID: roleID, X: x, Y: y})
	if err != nil {
		panic(err)
	}
	return treasure
}

func BossWin(roleID, x, y int32) *pb.Treasure {

	var treasure, err = TreasureService.BossWin(context.Background(), &service.ExploreRequest{RoleID: roleID, X: x, Y: y})
	if err != nil {
		panic(err)
	}
	return treasure
}

func Finish(treasure *pb.Treasure) *service.TreasureFinishResponse {

	var resp, err = TreasureService.Finish(context.Background(), treasure)
	if err != nil {
		panic(err)
	}
	return resp
}
